package top.conangao.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import top.conangao.common.exception.ApiException;
import top.conangao.system.convertor.SysRoleConvertor;
import top.conangao.system.convertor.SysUserConvertor;
import top.conangao.system.domain.SysRole;
import top.conangao.system.domain.SysUser;
import top.conangao.system.domain.SysUserRole;
import top.conangao.system.domain.dto.SysUserDTO;
import top.conangao.system.mapper.SysUserMapper;
import top.conangao.system.service.SysRoleService;
import top.conangao.system.service.SysUserRoleService;
import top.conangao.system.service.SysUserService;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author Admin
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2023-11-16 10:25:19
*/
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    
    private final SysUserConvertor sysUserConvertor;
    private final SysUserRoleService sysUserRoleService;
    private final SysRoleService sysRoleService;
    private final SysRoleConvertor sysRoleConvertor;

    @Override
    public boolean removeById(Serializable id) {
        // 删除用户角色关联
        sysUserRoleService.remove(sysUserRoleService.lambdaQuery().eq(SysUserRole::getUserId, id));
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean save(SysUser entity) {
        SysUser sysUser = lambdaQuery().eq(SysUser::getUsername, entity.getUsername()).one();
        if (!ObjectUtils.isEmpty(sysUser)){
            throw new ApiException(500,"用户名重复");
        }
        return super.save(entity);
    }

    @Override
    public SysUserDTO getUserByUsername(String username) {
        SysUserDTO sysUserDTO = sysUserConvertor.do2dto(lambdaQuery().eq(SysUser::getUsername, username).one());
        if (ObjectUtils.isEmpty(sysUserDTO)) {
            return sysUserDTO;
        }
        List<SysUserRole> sysUserRoleList =
                sysUserRoleService.lambdaQuery().eq(SysUserRole::getUserId, sysUserDTO.getId()).list();
        List<Long> rolesId = Optional.of(sysUserRoleList).orElse(Collections.emptyList())
                                     .stream().map(SysUserRole::getRoleId).toList();
        if (rolesId.isEmpty()) {
            return sysUserDTO;
        }
        List<SysRole> sysRoleList = sysRoleService.lambdaQuery().in(SysRole::getId, rolesId).list();
        sysUserDTO.setListSysRoleDTO(sysRoleConvertor.do2dto(sysRoleList));
//        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.lambdaQuery().in(SysRoleMenu::getRoleId, rolesId).list();
//        List<Long> sysMenusId = Optional.ofNullable(sysRoleMenuList).orElse(Collections.emptyList())
//                        .stream().map(SysRoleMenu::getMenuId).toList();
//        if (sysMenusId.isEmpty()){
//            return sysUserDTO;
//        }
//        List<SysMenu> sysMenuList = sysMenuService.lambdaQuery().in(SysMenu::getId, sysMenusId).list();
//        sysUserDTO.setListSysMenuDTO(sysMenuConvertor.do2dto(sysMenuList));
        return sysUserDTO;
    }
}




